package com.suse.common.util.mail;

import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * <p>
 * 邮件配置
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-29 19:48
 */
@Slf4j
public class MailUtil {

    private static final String FROM = "2404337367@qq.com";

    public static void sendMail(String to,String subject,String content) {
        try {
            JavaMailSender mailSender = SpringUtil.getBean(JavaMailSender.class);
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setFrom(FROM);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            mailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
            log.error("发送邮件失败: " + e.getMessage());
        }
    }

}
